/**   
 * @{#} Stage.java Create on 2013-7-23 下午3:34:36   
 *   
 * Copyright (c) 2012 by Baidu.   
 */
package union.func;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author <a href="mailto:zhangbo07@baidu.com">zhangbo07</a>
 * @version 1.0
 */

public class Stages {
	List<Stage> l = new ArrayList<Stage>();

	public void addStage(long low, long high, double rate, double basevalue) {
		l.add(new Stage(low, high, rate, basevalue));
		Collections.sort(l);
	}

	public double queryRate(long value) {
		// 默认值是1
		double result = 1.0d;
		// 展现为0，取最低档次分成比例
		if (value == 0 && l.size() > 0) {
			Stage lows = l.get(0);
			return lows.rate;
		} else {
			for (Stage s : l) {
				if (value >= s.low && value < s.high) {
					return ((value - s.low) * s.rate + s.basevalue) / value;
				}
			}
		}
		return result;
	}

	class Stage implements Comparable<Stage> {
		long low;
		long high;
		double rate;
		double basevalue;

		public Stage(long low, long high, double rate, double basevalue) {
			this.low = low;
			this.high = high;
			this.rate = rate;
			this.basevalue = basevalue;
		}

		public int compareTo(Stage o) {
			return this.low < o.low ? 0 : 1;
		}
	}
}
